package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.knowledge.base.pojo.dto.DeleteTagDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.GetTagListDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.GroupDeleteDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.SaveGroupDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.SaveTagDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.TagScreenListDTO;
import com.xbongbong.pro.knowledge.base.pojo.vo.GroupListVO;
import com.xbongbong.pro.knowledge.base.pojo.vo.TagListVO;
import com.xbongbong.pro.knowledge.base.pojo.vo.TagScreenListVO;
import com.xbongbong.pro.label.pojo.dto.LabelSortDTO;
import com.xbongbong.saas.service.KnowledgeTagService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author mingliang
 * @Description 知识库相关标签接口
 * @Date 2022/7/4 10:26
 **/
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/knowledge/tagSet")
public class KnowledgeTagSetController {

    @Resource
    private KnowledgeTagService knowledgeTagService;


    /**
     * @param saveTagDTO
     * @param br
     * @Author mingliang
     * @Description 新建编辑标签组
     * @Date 2022/7/4 15:57
     **/
    @RequestMapping(value = "/group/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String groupSave(@RequestBody @Valid SaveGroupDTO saveTagDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeTagService.saveGroup(saveTagDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @param baseDTO
     * @param br
     * @Author mingliang
     * @Description 标签组列表查询
     * @Date 2022/7/4 15:58
     **/
    @RequestMapping(value = "/group/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String groupList(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<GroupListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                GroupListVO groupList = knowledgeTagService.getGroupList(baseDTO);
                response = new XbbResponse<>(groupList);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @param groupDeleteDTO
     * @param br
     * @Author mingliang
     * @Description 删除标签组
     * @Date 2022/7/4 15:58
     **/
    @RequestMapping(value = "/group/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String groupDelete(@RequestBody @Valid GroupDeleteDTO groupDeleteDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeTagService.deleteGroup(groupDeleteDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @param getTagListDTO
     * @param br
     * @Author mingliang
     * @Description 标签列表
     * @Date 2022/7/4 15:58
     **/
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid GetTagListDTO getTagListDTO, BindingResult br) {
        XbbResponse<TagListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TagListVO tagListVO = knowledgeTagService.tagList(getTagListDTO);
                response = new XbbResponse<>(tagListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @param saveTagDTO
     * @Author mingliang
     * @Description 新建编辑标签
     * @Date 2022/7/4 15:58
     **/
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid SaveTagDTO saveTagDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeTagService.saveTag(saveTagDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @param deleteTagDTO
     * @Author mingliang
     * @Description 删除标签
     * @Date 2022/7/4 15:58
     **/
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid DeleteTagDTO deleteTagDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeTagService.deleteTag(deleteTagDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @param landSortDTO
     * @Author mingliang
     * @Description 标签组排序
     * @Date 2022/7/4 15:59
     **/
    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sort(@RequestBody @Valid LabelSortDTO landSortDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeTagService.sort(landSortDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description
     * @Date 2022/7/5 14:35
     * @param tagScreenListDTO
     * @param br
     **/
    @RequestMapping(value = "/tagList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String knowledgeTagList(@RequestBody @Valid TagScreenListDTO tagScreenListDTO, BindingResult br) {
        XbbResponse<TagScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TagScreenListVO tagScreenListVO = knowledgeTagService.getScreenList(tagScreenListDTO);
                response = new XbbResponse<>(tagScreenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
